#pragma once
#include <gfx/dx12/dx_common.h>
#include <gfx/dx12/helper.h>
#include <utl/vector.h>
#include <gfx/dx12/command.h>

struct GLFWwindow;

namespace primal
{
	namespace gfx
	{

		struct frame_buffer
		{
			ComPtr<ID3D12Resource> rt;
			descriptor_handle rt_handle;
			ComPtr<ID3D12Resource> ds;
			descriptor_handle ds_handle;
		};

		constexpr u64 FrameCount{ COMMAND_FRAME_COUNT };

		class SwapChain : public ComPtr<IDXGISwapChainN>, public GetComPtr<SwapChain>
		{
		public:
			SwapChain(GLFWwindow* window);
			SwapChain(const SwapChain&) = delete;
			SwapChain(const SwapChain&&) = delete;
			SwapChain& operator=(const SwapChain&) = delete;
			SwapChain& operator=(const SwapChain&&) = delete;
			//~SwapChain();
			void resize(u32 width, u32 height);
			void delayed_resize(u32 width, u32 height);
			void do_resize(DirectCMD* cmd);

			inline void present();
			void begin(ID3D12GraphicsCommandListN* list);
			void end(ID3D12GraphicsCommandListN* list);
		private:
			u32 _flag{};
			u32 _sync_interval{};
			d3dx::swapchain_desc _desc{};
			DXGI_PRESENT_PARAMETERS _parameters{};
			D3D12_VIEWPORT _viewport{};
			D3D12_RECT _scissor_rect{};
			frame_buffer frames[FrameCount]{};
			d3dx::resource_barrier barrier{};
			u64 current_index{};

			float backcolor[4]{};
			float timer{};

			HANDLE sync_event;

			struct
			{
				u32 width;
				u32 height;
				bool is_resize;
			}resize_parameter{};
		};
	}
}